<template>
<section v-loading="list_loading">
  <div class="searchbox">
      <el-form inline :model="query"  class="demo-form-inline fl">
        <el-form-item>
          <el-input size="medium" v-model="query.k" placeholder="教练姓名或手机号"></el-input>
        </el-form-item>
        <el-form-item>
          <el-select size="medium" v-model="query.type" placeholder="选择归属科目">
            <el-option label="全部科目" value=""></el-option>
            <el-option label="全职" value="0"></el-option>
            <el-option label="科二" value="2"></el-option>
            <el-option label="科三" value="3"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" size="small" icon="el-icon-search" @click="doSearch">搜&nbsp;&nbsp;索</el-button>
        </el-form-item>
      </el-form>
      <el-button type="primary" size="small" class="fr" icon="el-icon-plus" @click.stop="addCoach">添加教练</el-button>
    </div>
    <div @contextmenu="$parent.addMenu([{name:'刷新教练列表',method:'getData',icon:'icon-shuaxin'}])">
      <el-table :data="listData" tooltip-effect="dark" highlight-current-row style="width: 100%;text-align:center" @row-click="openSidebar" @selection-change="rowSelect">
          <el-table-column type="selection" width="55"></el-table-column>
          <el-table-column prop="id" label="教练ID"></el-table-column>
        	<el-table-column prop="real_name" label="姓名"></el-table-column>
          <el-table-column label="性别">
            <template slot-scope="scope">
                <span v-if="scope.row.sex==0">未知</span>
                <i v-else-if="scope.row.sex==1" class="iconfont icon-nanxing" style="color:#31C8A4"></i>
                <i v-else="scope.row.sex==2" class="iconfont icon-nvxing" style="color:#ea727f"></i>
            </template>
          </el-table-column>
        	<el-table-column prop="mobile" label="手机号"></el-table-column>
        	<el-table-column prop="money" label="资金(￥)"></el-table-column>
          <el-table-column label="归属科目">
            <template slot-scope="scope">{{ scope.row.kemu|kemu }}</template>
          </el-table-column>
          <el-table-column label="状态">
            <template slot-scope="scope">
                <span v-if="scope.row.status==0">正常</span>
                <span v-else>请假</span>
            </template>
          </el-table-column>
      </el-table>
      <div class="breakpage" v-if="page.total>0">
        <el-select size="medium" v-model="coach_kemu" placeholder="批量修改归属科目" @change="changeProgress" style="float:left;width:160px">
          <el-option label="全职" value="0"></el-option>
          <el-option label="科目二" value="2"></el-option>
          <el-option label="科目三" value="3"></el-option>
        </el-select>
        <el-pagination background layout="total,sizes,prev, pager, next"
            :total="parseInt(page.total)"
            :page-size="parseInt(page.pagesize)"
            :current-page="parseInt(page.curpage)"
            @size-change="sizeChange"
            @current-change="pageChange">
        </el-pagination>
      </div>
    </div>
<!--侧边栏-->
    <transition name="slide-fade">
  	  <div v-show="sidebar.show" class="sidebar" v-loading="sidebar.loading" @contextmenu="$parent.addMenu([{name:'关闭侧边栏',method:'closeSidebar',icon:'icon-guanbi'}])">
  	    <el-tabs class="coach_side" @tab-click="tabChange" value="info">
  			  <el-tab-pane label="教练信息" name="info">
            <div v-if="sidebar.info" class="coach-infor" @contextmenu="$parent.addMenu([{name:'刷新教练信息',method:'getSideData=info',icon:'icon-shuaxin'}])">
            		<ul>
            			<li class="clearfix"><span class="coach-att">姓名</span><span class="coach-val">{{sidebar.info.real_name}}</span></li>
            			<li class="clearfix"><span class="coach-att">手机号</span><span class="coach-val">{{sidebar.info.mobile}}</span></li>
            			<li class="clearfix"><span class="coach-att">身份证号</span><span class="coach-val">{{sidebar.info.id_number}}</span></li>
            			<li class="clearfix"><span class="coach-att">资金</span><span class="coach-val">{{sidebar.info.money}}元</span></li>
                  <li class="clearfix"><span class="coach-att">冻结资金</span><span class="coach-val">{{sidebar.info.frozen_money}}元</span></li>
            			<li class="clearfix"><span class="coach-att">归属科类</span><span class="coach-val">{{sidebar.info.kemu|kemu}}</span></li>
            			<li class="clearfix"><span class="coach-att">状态</span><span class="coach-val">正常</span></li>
            			<li class="clearfix"><span class="coach-att">注册时间</span><span class="coach-val">{{sidebar.info.regtime|date('Y-m-d')}}</span></li>
            			<li class="clearfix"><span class="coach-att">登录时间</span><span class="coach-val">{{sidebar.info.lasttime|date('Y-m-d')}}</span></li>
            			<li class="clearfix" v-if="sidebar.info.file" style="height:120px;"><span class="coach-att">教练资料</span>
            			<div class="coach-img clearfix">
            					<img v-for="path in sidebar.info.file" :src="path" style="cursor:pointer" @click.stop='imgPreview({url:path})'>
            			</div>
            			</li>
                  <li class="clearfix"><span class="coach-att">&nbsp;</span>
                    <el-button type="primary" size="small" @click.stop="editCoach">编 &nbsp; 辑</el-button>
                    <el-button type="danger" size="small" @click.stop="deleteCoach">删 &nbsp; 除</el-button>
                  </li>
            		</ul>
	          </div>
  			  </el-tab-pane>
  			  <el-tab-pane label="预约信息" name="bookList">
            <div style="padding:0 60px" @contextmenu="$parent.addMenu([{name:'刷新预约信息',method:'getSideData=bookList',icon:'icon-shuaxin'}])">
              <p style="text-align:center;margin:10px 0">
                <el-button type="text" :disabled="day.pre" @click="preDay"><i class="el-icon-arrow-left" style="font-size:25px"></i></el-button>
                <span style="font-size:22px;color:#666;padding:0 10px">{{sidebar.bookList.date}}</span>
                <el-button type="text" :disabled="day.next" @click="nextDay"><i class="el-icon-arrow-right" style="font-size:25px"></i></el-button>
              </p>
              <p style="text-align:center;line-height:50px">上午</p>
              <el-table :data="sidebar.bookList.am" border style="width: 100%;text-align:center">
                  <el-table-column prop="real_name" label="学员姓名"></el-table-column>
                  <el-table-column prop="mobile" label="手机号"></el-table-column>
                  <el-table-column label="预约科目">
                    <template slot-scope="scope">{{ scope.row.book_kemu|kemu }}</template>
                  </el-table-column>
                  <el-table-column label="预约时间">
                    <template slot-scope="scope">{{ scope.row.book_time+':00-'+scope.row.book_time+':59' }}</template>
                  </el-table-column>
              </el-table>
              <p style="text-align:center;line-height:50px;margin-top:10px">下午</p>
              <el-table :data="sidebar.bookList.pm" border style="width: 100%;text-align:center">
                  <el-table-column prop="real_name" label="学员姓名"></el-table-column>
                  <el-table-column prop="mobile" label="手机号"></el-table-column>
                  <el-table-column label="预约科目">
                    <template slot-scope="scope">{{ scope.row.book_kemu|kemu }}</template>
                  </el-table-column>
                  <el-table-column label="预约时间">
                    <template slot-scope="scope">{{ scope.row.book_time+':00-'+scope.row.book_time+':59' }}</template>
                  </el-table-column>
              </el-table>
            </div>
          </el-tab-pane>
  			  <el-tab-pane label="教练学员" name="students">
            <div @contextmenu="$parent.addMenu([{name:'刷新教练学员',method:'getSideData=students',icon:'icon-shuaxin'}])">
              <el-table :data="sidebar.students" style="width: 100%;text-align:center">
                  <el-table-column prop="id" label="学员id"></el-table-column>
                  <el-table-column prop="real_name" label="姓名"></el-table-column>
                  <el-table-column prop="mobile" label="手机号"></el-table-column>
                  <el-table-column label="状态">
                    <template slot-scope="scope">{{ scope.row.kemu}}</template>
                  </el-table-column>
              </el-table>
            </div>
          </el-tab-pane>
  			  <el-tab-pane label="资金记录" name="account">
            <div @contextmenu="$parent.addMenu([{name:'刷新资金记录',method:'getSideData=account',icon:'icon-shuaxin'}])">
              <el-table :data="sidebar.account" style="width: 100%;text-align:center">
                  <el-table-column label="时间">
                    <template slot-scope="scope">{{ scope.row.regtime|date('Y-m-d H:i') }}</template>
                  </el-table-column>
                  <el-table-column prop="user_balance" label="资金余额" width="180"></el-table-column>
                  <el-table-column label="收入/支出">
                    <template slot-scope="scope">
                        <font v-if="scope.row.change_type==1" style="color:green">+{{ scope.row.deal_amount }}</font>
                        <font v-else style="color:red">-{{ scope.row.deal_amount }}</font>
                    </template>
                  </el-table-column>
                  <el-table-column prop="deal_name" label="交易类型"></el-table-column>
              </el-table>
            </div>
          </el-tab-pane>
          <el-tab-pane label="教练日志" name="leave">
            <div @contextmenu="$parent.addMenu([{name:'刷新教练日志',method:'getSideData=leave',icon:'icon-shuaxin'}])">
              <el-table :data="sidebar.leave" style="width: 100%;text-align:center">
                  <el-table-column label="请假开始时间">
                    <template slot-scope="scope">{{ scope.row.begin_time|date('Y-m-d H:i') }}</template>
                  </el-table-column>
                  <el-table-column label="请假结束时间">
                    <template slot-scope="scope">{{ scope.row.end_time|date('Y-m-d H:i') }}</template>
                  </el-table-column>
                  <el-table-column prop="duration" label="时长"></el-table-column>
                  <el-table-column prop="real_name" label="审批人"></el-table-column>
              </el-table>
            </div>
          </el-tab-pane>
        </el-tabs>
  	  </div>
    </transition>
    <div v-if="sidebar.show" class="v-modal" @click="closeSidebar"></div>
<!--侧边栏-->
<!--添加弹窗-->
<el-dialog :title="coachForm.type=='add'?'添加教练':'编辑教练'" :visible.sync="coachForm.show" width="600px">
  <el-form :model="coachForm.data" :rules="rules" ref="coachForm" label-width="80px">
  <el-form-item label="姓名" prop="real_name">
    <el-input v-model="coachForm.data.real_name" placeholder="请输入教练姓名"></el-input>
  </el-form-item>
  <el-form-item label="手机号" prop="mobile">
    <el-input v-model="coachForm.data.mobile" placeholder="请输入教练手机号" required></el-input>
  </el-form-item>
  <el-form-item label="身份证号" prop="id_number">
    <el-input v-model="coachForm.data.id_number" placeholder="请输入教练身份证号" required></el-input>
  </el-form-item>
  <el-form-item label="归属科类" prop="kemu">
    <el-col :span="10">
      <el-select v-model="coachForm.data.kemu" placeholder="请选择科类">
        <el-option label="全职" value="0"></el-option>
        <el-option label="科目二" value="2"></el-option>
        <el-option label="科目三" value="3"></el-option>
      </el-select>
    </el-col>
    <el-col :span="12">
      <el-form-item label="教龄">
       <el-input-number v-model="coachForm.data.t_year" :min="1" :max="30" label="教龄"></el-input-number>
       </el-form-item>
    </el-col>
  </el-form-item>
  <el-form-item label="教练简介">
      <el-input type="textarea" :rows="3"  placeholder="请输入教练简介" v-model="coachForm.data.brief"></el-input>
  </el-form-item>
  <el-form-item label="上传资料">
    <el-upload class="file-uploader" :action="$parent.uploadUrl" :headers="{token:$parent.token}" accept="image/*" :show-file-list="false" name="img_3" :on-success="uploadSuccess">
        <div v-if="coachForm.data.file&&coachForm.data.file.img_3" class="avatar" @click.stop>
             <img :src="coachForm.data.file.img_3"><p>教练资格证</p>
             <span class="file_item_action">
                <i class="el-icon-zoom-in" @click.stop='imgPreview({url:coachForm.data.file.img_3})'></i>
                <i class="el-icon-delete" @click.stop='removeFile("img_3")'></i>
             </span>
        </div>
        <div v-else><i class="el-icon-plus avatar-uploader-icon"></i><p>教练资格证</p></div>
    </el-upload>
    <el-upload class="file-uploader" :action="$parent.uploadUrl" :headers="{token:$parent.token}" accept="image/*" :show-file-list="false" name="img_1" :on-success="uploadSuccess">
        <div v-if="coachForm.data.file&&coachForm.data.file.img_1" class="avatar" @click.stop>
             <img :src="coachForm.data.file.img_1" ><p>身份证正面</p>
             <span class="file_item_action">
                <i class="el-icon-zoom-in" @click.stop='imgPreview({url:coachForm.data.file.img_1})'></i>
                <i class="el-icon-delete" @click.stop='removeFile("img_1")'></i>
             </span>
        </div>
        <div v-else><i class="el-icon-plus avatar-uploader-icon"></i><p>身份证正面</p></div>
    </el-upload>
    <el-upload class="file-uploader" :action="$parent.uploadUrl" :headers="{token:$parent.token}" accept="image/*" name="img_2" :show-file-list="false" :on-success="uploadSuccess">
        <div v-if="coachForm.data.file&&coachForm.data.file.img_2" class="avatar" @click.stop>
             <img :src="coachForm.data.file.img_2" ><p>身份证反面</p>
             <span class="file_item_action">
                <i class="el-icon-zoom-in" @click.stop='imgPreview({url:coachForm.data.file.img_2})'></i>
                <i class="el-icon-delete" @click.stop='removeFile("img_2")'></i>
             </span>
        </div>
        <div v-else><i  class="el-icon-plus avatar-uploader-icon"></i><p>身份证反面</p></div>
    </el-upload>
  </el-form-item>
  </el-form>
  <div slot="footer" class="dialog-footer">
    <el-button size="small" @click="coachForm.show = false">取 消</el-button>
    <el-button size="small" type="primary" @click.stop="submitCoachForm" :loading="coachForm.loading">保 存</el-button>
  </div>
</el-dialog>
<!--添加弹窗-->
<!--图片预览-->
<el-dialog :visible.sync="preview.show"><img width="100%" :src="preview.path"></el-dialog>
</section>
</template>

<script>
import * as api from '../api/index'
import {date,checkIDCard,checkMobile} from '../lib/function'
var validateMobile = (rule, value, callback) => {
    if (value==''||value==undefined) {
      callback(new Error('请输入手机号'));
    } else if(!checkMobile(value)){
      callback(new Error('手机号格式不正确'));
    }
    callback();
};
var validateIdNum = (rule, value, callback) => {
    if (value==''||value==undefined) {
      callback(new Error('请输入身份证号'));
    } else if(!checkIDCard(value)){
      callback(new Error('身份证号格式不正确'));
    }
    callback();
};
export default {
	  name:'coach',//页面名称
    data() {
      return {
    		m:'coach',
        query:{},//列表搜索条件
        select:[],
        coach_kemu:'',
        sidebar:{//侧边栏
          show:false,
          curtab:'info',
          loading:false,
          id:0,
          info:{},
          bookList:[],
          students:[],
          account:[],
          leave:[]
        },
        day:{i:0,pre:true,next:false},
        preview:{show:false,path:''},//图片预览
        coachForm:{//添加编辑教练的表单数据
          show:false,//弹窗显示状态
          loading:false,
          type:'add',
          data:{}//表单数据
        },
        rules: {
          real_name:[
             { required: true, message: '请输入教练姓名', trigger: 'blur' },
              { min: 3, max: 5, message: '长度在 3 到 5 个字符', trigger: 'blur' }
            ],
          mobile:[
            { required: true, message: '请输入手机号', trigger: 'blur' },
            {validator:validateMobile,trigger: 'blur' },
          ],
          id_number:[
            { required: true, message: '请输入身份证号', trigger: 'blur' },
            { validator:validateIdNum, trigger: 'blur' }
          ],
          kemu: [
             { required: true, message: '请选择归属科类', trigger: 'blur' }
            ],
        }
      }
    },
	 computed:{
      listData(){
          return this.$store.state[this.m].data;
      },
  		page(){
  			  return this.$store.state[this.m].page;
  		},
      list_loading(){
          return this.$store.state[this.m].list_loading;
      }
    },
    filters:{
      date,
      kemu(d){
          switch(parseInt(d)){
            case 0:return '全职';break;
            case 2:return '科目二';break;
            case 3:return '科目三';break;
            default:return '其它';
          }
      }
	  },
    methods: {
      removeFile(n){
        this.coachForm.data.file[n]='';
        this.$forceUpdate();
      },
      uploadSuccess(res, file, fileList){
        if(res.status==1){
          let key=Object.keys(res.data)[0];
          if(this.coachForm.data.file==undefined) this.coachForm.data.file={};
          this.coachForm.data.file[key]=res.data[key].path;
          this.$forceUpdate();
        }
      },
      imgPreview(file) {
       this.preview.path = file.url;
       this.preview.show = true;//是否预览
     },
     rowSelect(s){
       this.select=[];
       s.forEach((item)=>{
         this.select.push(item.id);
       });
     },
     changeProgress(kemu){
       if(this.select.length<=0) {
         this.$message.warning({message:'请先选择需要修改归属科目的教练'});
         this.coach_kemu='';
         return;
       }
       let a={0:'全职',2:'科目二',3:'科目三'};
       this.$confirm('确定将已选中教练归属科目修改为【'+a[kemu]+'】?','提示',{confirmButtonText:'确定',cancelButtonText:'取消',type: 'warning'
          }).then(() => {
             api.coach.modifyKemu({ids:this.select,kemu},res=>{
               if(res.status==1){
                 this.getData();
                 this.coach_kemu='';
               }else this.$notify.error(res);
             });
         }).catch(()=>{this.coach_kemu=''});
      },
      openSidebar(row, event, column){
        if(column==undefined||column.type!='default') return;
         this.sidebar.show=true;
         if(row.id!=this.sidebar.id){
           this.sidebar.id=row.id;
           this.sidebar.info={};
           this.sidebar.students=this.sidebar.bookList=this.sidebar.account=this.sidebar.log=[];
           this.getSideData(this.sidebar.curtab);
         }
      },
      closeSidebar(){this.sidebar.show=false},
      tabChange(e){
        this.sidebar.curtab=e.name;
        if(Object.keys(this.sidebar[e.name])<=0) {
            this.getSideData(e.name);
        }
      },
      preDay(){
        this.day.i--;
        if(this.day.i==0) {
          this.day.pre=true;
          this.day.next=false;
        }
        this.getSideData('bookList');
      },
      nextDay(){
        this.day.i++;
        if(this.day.i==2) {
          this.day.next=true;
          this.day.pre=false;
        }
        this.getSideData('bookList');
      },
      getSideData(name){
        this.sidebar.loading=true;
        let param={id:this.sidebar.id};
        if(name=='bookList') param.i=this.day.i;
        api[this.m][name](param,res=>{
           if(res.status==1){
              this.sidebar[name]=res.data;
           }else{
             this.$notify.error(res);
           }
            this.sidebar.loading=false;
        });
      },
      addCoach(){
        if(this.coachForm.type=='edit'){
          this.coachForm.type='add';
          this.coachForm.data={};
        }
        this.coachForm.show=true;
      },
      editCoach(){
         this.coachForm.type='edit';
         if(!this.coachForm.show){
            this.coachForm.show=true;
            this.coachForm.data=JSON.parse(JSON.stringify(this.sidebar.info));
         }
       },
       deleteCoach() {
          this.$confirm("此操作将永久删除, 是否继续?", "提示", {
                  confirmButtonText: "确定",
                  cancelButtonText: "取消",
                  type: "warning"
                })
                  .then(() => {
                    this.$store.dispatch(this.m + "/delListData", this.sidebar.info.id);
                    this.closeSidebar();
                    this.$message({
                      type: "success",
                      message: "删除成功!"
                    });
                  });
       },
      submitCoachForm() {//提交教练表单
        this.$refs['coachForm'].validate((valid) => {
          if (valid) {
            this.coachForm.loading=true;
            api.coach[this.coachForm.type](this.coachForm.data,res=>{
                this.coachForm.loading=false;
                if(res.status==1){
                  this.coachForm.show=false;
                  this.coachForm.data={};
                  if(this.coachForm.type=='edit') this.getSideData('info');
                  this.getData();
                }else{
                  this.$notify.error(res);
                }
            });
          } else {
            return false;
          }
        });
      },
      doSearch(){//查询数据
        this.query.p=1;
        this.getData();
      },
      sizeChange(size){
         this.query.pagesize=size;
         this.getData();
      },
      pageChange(p){
        this.query.p=p;
        this.getData();
      },
  	  getData(){//获取教练列表数据
  			this.$store.dispatch(
  			    this.m+'/getListData',
  				  JSON.parse(JSON.stringify(this.query))
  			);
  		}
    },
	created(){
    this.getData();
      // if(this.$parent.school.current.id==undefined||this.$store.state[this.m].school_id!=this.$parent.school.current.id) {
      //   this.query={};
      //   this.$store.commit(this.m+'/update',{type:'school_id',data:this.$parent.school.current.id});
      // }else{
      //   this.query=JSON.parse(JSON.stringify(this.$store.state[this.m].query));
      // }
    }
  }
</script>
